<template>
  <div>
    <div class="add_wrap">
      <div class="tit">
        <h2>条码号信息</h2>
        <el-button
          plain
          size="small"
          type="success"
          icon="fa fa-mail-forward"
          @click="returnList"
        >
          返 回</el-button
        >
      </div>
      <el-form
        :model="preForm"
        ref="preForm"
        label-width="200px"
        :rules="preRules"
      >
        <el-row>
          <el-form-item label="医检名称" prop="name">
            <el-select
              v-model="preForm.institutionName"
              filterable
              clearable
              reserve-keyword
              placeholder="请选择医检机构"
              @click.native="institutionChange"
              @change="currentSel"
            >
              <el-option
                v-for="item in institutionList"
                :key="item.institutionId"
                :label="item.institutionName"
                :value="item.institutionId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="条码号前缀" prop="codePre">
            <el-input v-model="preForm.codePre"></el-input>
          </el-form-item>
          <el-form-item label="最小长度" prop="lengthMin">
            <el-input v-model="preForm.lengthMin"></el-input>
          </el-form-item>
          <el-form-item label="最大长度" prop="lengthMax">
            <el-input v-model="preForm.lengthMax"></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input
              type="textarea"
              v-model="preForm.remark"
              style="width: 800px"
            ></el-input>
          </el-form-item>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12" :offset="6">
            <el-button
              v-if="!isUpdate"
              plain
              type="primary"
              @click="surePreClick"
              >保 存</el-button
            >
            <el-button
              v-if="isUpdate"
              plain
              type="primary"
              @click="surePreClickUpdate"
              >保 存</el-button
            >
            <el-button @click="returnList">取 消</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import * as message from "@/constants/message";
import BarodeApi from "@/api/institution/barode";
import InstitutionApi from "@/api/institution/institution";
export default {
  data() {
    return {
      preForm: {
        institutionName: "",
        codePre: "",
        lengthMin: "",
        lengthMax: "",
        remark: "",
        state: 1
      },
      insCodeRuleId: "",
      selectValue: "",
      isUpdate: false,
      //接受修改的传参
      rowData: [],
      preRules: {
        name: [
          {
            required: true,
            message: "请选择医检机构",
            trigger: "blur"
          }
        ],
        codePre: [
          {
            message: "请输入条形码前缀",
            trigger: "blur"
          }
        ],
        lengthMin: [
          {
            required: true,
            message: "请输入条形码最小长度",
            trigger: "blur"
          }
        ],
        lengthMax: [
          {
            required: true,
            message: "请输入条形码最大长度",
            trigger: "blur"
          }
        ]
      },
      preDisable: false,
      institutionList: []
    };
  },
  mounted() {
    this.rowData = this.$route.params.rowData;
    if (this.rowData) {
      this.insCodeRuleId = this.rowData.insCodeRuleId;
      this.selectValue = this.rowData.institutionId;
      this.isUpdate = true;
      this.preForm.institutionName = this.rowData.institutionName;
      this.preForm.codePre = this.rowData.codePre;
      this.preForm.lengthMin = this.rowData.lengthMin;
      this.preForm.lengthMax = this.rowData.lengthMax;
      this.preForm.remark = this.rowData.remark;
      this.preForm.state = this.rowData.state;
      delete this.preRules.name;
    } else {
      this.isUpdate = false;
    }
  },
  methods: {
    currentSel(val) {
      this.selectValue = val;
      if (this.selectValue != "") {
        delete this.preRules.name;
      }
    },
    institutionChange() {
      let _this = this;
      InstitutionApi.getInstitutionNameList().then(res => {
        _this.institutionList = res.data;
      });
    },
    getBarodeData() {
      let _this = this;
      let code = {
        codePre: _this.preForm.codePre,
        lengthMin: _this.preForm.lengthMin,
        lengthMax: _this.preForm.lengthMax,
        remark: _this.preForm.remark
      };
      BarodeApi.getBarode(code).then(res => {
        _this.$Utils.bindingBean(_this.preForm, res.data);
      });
    },
    //新增保存
    surePreClick() {
      this.$refs.preForm.validate(valid => {
        if (valid) {
          this.$confirm(
            message.MSG_CONFIRM_ADD_BARODE_RULE,
            message.TITLE_PROMPT,
            {}
          ).then(() => {
            this.getBarode();
          });
        }
      });
    },
    //修改保存
    surePreClickUpdate() {
      this.$refs.preForm.validate(valid => {
        if (valid) {
          this.$confirm(
            message.MSG_CONFIRM_SAVE_ALTER,
            message.TITLE_PROMPT,
            {}
          ).then(() => {
            this.getBarodeUpdate();
          });
        }
      });
    },
    //发送请求
    getBarode() {
      let _this = this;
      let barCodeData = {
        institutionId: _this.selectValue,
        codePre: _this.preForm.codePre,
        lengthMin: _this.preForm.lengthMin,
        lengthMax: _this.preForm.lengthMax,
        state: _this.preForm.state,
        remark: _this.preForm.remark
      };
      BarodeApi.addBarode(barCodeData).then(res => {
        this.$Utils.notifySuccess();
        _this.returnList();
      });
    },
    //发送修改请求
    getBarodeUpdate() {
      let _this = this;
      let barodeData = {
        insCodeRuleId: _this.insCodeRuleId,
        institutionId: _this.selectValue,
        codePre: _this.preForm.codePre,
        lengthMin: _this.preForm.lengthMin,
        lengthMax: _this.preForm.lengthMax,
        state: _this.preForm.state,
        remark: _this.preForm.remark
      };
      BarodeApi.upBarode(barodeData).then(res => {
        this.$Utils.notifySuccess();
        _this.returnList();
      });
    },
    //取消
    returnList() {
      this.$store.dispatch("delView", this.$route);
      this.$router.push({ path: "/institu/barcodeManagement" });
    }
  }
};
</script>

<style lang="scss" src="@/assets/css/views/add.scss" scoped></style>
<style scoped>
.add_wrap {
  padding-top: 10px;
  padding-left: 23px;
}
</style>
